package com.huiquan.icd.service;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.BaseService;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.*;
import com.huiquan.icd.dao.IcdDiseaseHospitalDao;
import com.huiquan.icd.dao.IcdDiseaseNationalStdDao;
import com.huiquan.icd.domain.IcdDiseaseHospital;
import com.huiquan.icd.domain.IcdDiseaseNationalStd;
import com.huiquan.icd.domain.IcdDiseaseSynonymy;
import com.huiquan.synonymy.dao.SynonymyDao;
import com.huiquan.synonymy.dao.SynonymyWordDao;
import com.huiquan.synonymy.domain.Synonymy;
import com.huiquan.synonymy.domain.SynonymyWord;
import com.huiquan.vocab.constant.VocabConstant;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import java.util.regex.Pattern;

@Service
public class IcdDiseaseHospitalService extends BaseService {

	@Autowired
	private IcdDiseaseHospitalDao icdCDiseaseHospitalDao;
	@Autowired
	private IcdDiseaseNationalStdDao icdDiseaseNationalStdDao;
	@Autowired
	private SynonymyWordDao synonymyWordDao;
	@Autowired
	private SynonymyDao synonymyDao;

	public ModelAndView list(Integer start, String codeSearch, String nameSearch, String exactSearchFlag, String type,
			String checkFlag, String countPerPageStr, String stdSearch, String categorySearch) {
		boolean exactFlag = exactSearchFlag != null && exactSearchFlag.equals("1");

		Map<String, Object> param = new HashMap<>();
		if (codeSearch != null && !codeSearch.isEmpty()) {
			param.put("diseaseCode", exactFlag ? codeSearch : "%" + codeSearch + "%");
		}
		if (nameSearch != null && !nameSearch.isEmpty()) {
			param.put("nameSearch", exactFlag ? nameSearch : "%" + nameSearch + "%");
		}
		if (stdSearch != null && !stdSearch.isEmpty()) {
			param.put("diseaseStd", exactFlag ? stdSearch : "%" + stdSearch + "%");
		}
		if (categorySearch != null && !categorySearch.isEmpty()) {
			param.put("diseaseCategory", exactFlag ? categorySearch : "%" + categorySearch + "%");
		}
		int totalSize = 0;
		if (StringUtils.equalsIgnoreCase(checkFlag, "1")) {
			param.put("disable", "1");
			param.put("checkFlag", "1");
			param.put("orderIndex", "c.gmt_modified desc");
			// checkFlag=2代表错误有效数据disable 2 无效
			totalSize = icdCDiseaseHospitalDao.retrieveAllRightSize(param);
		} else if (StringUtils.equalsIgnoreCase(checkFlag, "2")) {
			param.put("disable", "2");
			param.put("checkFlag", "1");
			param.put("orderIndex", "c.gmt_modified desc");
			totalSize = icdCDiseaseHospitalDao.retrieveSize(param);
		}

		Map<String, Integer> pageNo = new HashMap<>();
		if (countPerPageStr != null && !countPerPageStr.isEmpty()) {
			int countPerPage = Integer.parseInt(countPerPageStr);
			pageNo = GetListUtil.getPageNoParam(totalSize, start + "", countPerPage);
			param.put("countPerPage", countPerPage);
		} else {
			pageNo = GetListUtil.getPageNoParam(totalSize, start + "");
		}
		int endPage = pageNo.get("endPage");
		start = pageNo.get("start");
		int startIndex = pageNo.get("startIndex");

		List<IcdDiseaseHospital> list = new ArrayList<>();

		if (startIndex < totalSize) {
			param.put("startIndex", startIndex);
			if (StringUtils.equalsIgnoreCase(checkFlag, "1")) {
				list = icdCDiseaseHospitalDao.retrievesListAllRight(param);
			} else {
				list = icdCDiseaseHospitalDao.retrieveList(param);
			}
		}
		// 例句根据char3分割
		for (IcdDiseaseHospital icdDisease : list) {
			if (!"2".equals(checkFlag)) {
				if (icdDisease.getHospitalId().split(",").length > 1) {
					icdDisease.setHospitalIdShow(icdDisease.getHospitalId().replace(",", "\n"));
					icdDisease.setHospitalId(icdDisease.getHospitalId().split(",")[0] + ",");
				}
				if (icdDisease.getDepartmentId().split(",").length > 1) {
					icdDisease.setDepartmentIdShow(icdDisease.getDepartmentId().replace(",", "\n"));
					icdDisease.setDepartmentId(icdDisease.getDepartmentId().split(",")[0] + ",");
				}
			}
			String value = icdDisease.getSentence();
			char splitWord = 3;
			if (StringUtils.isNotBlank(value)) {
				String[] sentences = value.split("\\" + splitWord);
				if (sentences.length > 0) {
					StringBuffer sentence = new StringBuffer();
					for (int i = 0; i < sentences.length; i++) {
						sentence.append(sentences[i]);
						if (i != (sentences.length - 1)) {
							sentence.append("\n");
						}
					}
					icdDisease.setSentence(sentence.toString());
					icdDisease.setShowSentence(sentences[0]);
				} else {
					icdDisease.setSentence("");
					icdDisease.setShowSentence("");
				}

			}
		}
		List<Map<String, String>> disableDatas = icdCDiseaseHospitalDao.checkDisable();
		StringBuffer disable = new StringBuffer();
		for (Map<String, String> disableData : disableDatas) {
			disable.append(disableData.get("disease_name") + ";");
		}
		List<Map<String, String>> categoryDisableDatas = icdCDiseaseHospitalDao.checkCategory();
		StringBuffer categoryDisableErr = new StringBuffer();
		for (Map<String, String> categoryDisableData : categoryDisableDatas) {
			categoryDisableErr.append(categoryDisableData.get("disease_name") + ";");
		}
		Map<String, String> showSearch = new LinkedHashMap<>();
		showSearch.put("codeSearch", "icd10编码");
		showSearch.put("nameSearch", "诊断描述");
		showSearch.put("stdSearch", "疾病名");
		showSearch.put("categorySearch", "一级");

		Map<String, String> searchMap = new HashMap<>();
		searchMap.put("codeSearch", codeSearch);
		searchMap.put("nameSearch", nameSearch);
		searchMap.put("stdSearch", stdSearch);
		searchMap.put("categorySearch", categorySearch);

		Map<String, Object> map = GetListUtil.getReturnMap2(totalSize, start, startIndex, endPage, list, searchMap);
		map.put("disableErr", disable.toString());
		map.put("categoryDisableErr", categoryDisableErr.toString());
		map.put("showSearch", showSearch);
		map.put("showSearchValue", searchMap);
		map.put("exactSearchFlag", exactSearchFlag);
		return new ModelAndView("icd/index", map);
	}

	public ModelAndView preEdit(String id) {
		Map<String, Object> map = new HashMap<>();
		map.put("ids", id);
		if (id.contains(",")) {
			id = id.split(",")[0];
		}
		IcdDiseaseHospital value = icdCDiseaseHospitalDao.retrieveObjectById(Long.parseLong(id));
		map.put("category", value.getCategory());
		map.put("disable", value.getDisable());
		map.put("diseaseCode", value.getDiseaseCode());
		map.put("gpName", value.getGpName());
		map.put("pName", value.getpName());
		map.put("departmentId", value.getDepartmentId());
		map.put("hospitalId", value.getHospitalId());
		map.put("std", value.getStd());
		map.put("diseaseName", value.getDiseaseName());
		map.put("id", id);
		map.put("delFlag", value.getDelFlag());
		map.put("chapterSequence", value.getChapterSequence());
		map.put("chapterCode", value.getChapterCode());
		map.put("chapterName", value.getChapterName());
		map.put("sectionCode", value.getSectionCode());
		map.put("sectionName", value.getSectionName());
		map.put("gpCode", value.getGpCode());
		map.put("gpAccessoryCode", value.getGpAccessoryCode());
		map.put("gpName", value.getGpName());
		map.put("suborderCode", value.getSuborderCode());
		map.put("suborderAccessoryCode", value.getSuborderAccessoryCode());
		map.put("suborderName", value.getSuborderName());
		map.put("similarDisease", value.getSimilarDisease());
		map.put("modifyCode", value.getModifyCode());

		return new ModelAndView("icd/disease_hospital_edit", map);
	}

	public ReturnData changeDisable(String id, User user, String disable, String diseaseName) {
		if (StringUtils.isNotBlank(disable) && user != null) {
			// 改变bac_icd_disease中的disable 和hedden_flag
			if (StringUtils.equals(disable, "2") || StringUtils.equals(disable, "0")
					|| StringUtils.equals(disable, "1")) {
				// 相同疾病名称的都改变 --但不改变为3错误的
				icdCDiseaseHospitalDao.changeDisable(id.split(",")[0], user.getUserId(), disable, diseaseName);
				// 如果是无效的把这些变为有效
				IcdDiseaseHospital icd = icdCDiseaseHospitalDao.retrieveObjectById(Long.parseLong(id.split(",")[0]));
				if ("1".equals(icd.getDelFlag())) {
					icdCDiseaseHospitalDao.changeWrong(id, user.getUserId(), "0");
				}
				// 如果是致为无效则把该疾病名的一级置空
				if (StringUtils.equals(disable, "2")) {
					icdCDiseaseHospitalDao.updateCategory("0", icd.getStd());
				}
			} else {
				// 只改变该条数据
				// 修改成无效数据
				icdCDiseaseHospitalDao.changeWrong(id, user.getUserId(), "1");
			}
		}
		return ReturnUtil.success();
	}

	public ReturnData submitChangeDisable(String[] ids, String[] disables, User user) {
		for (String disable : disables) {
			if (StringUtils.isBlank(disable)) {
				ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
				rc.setDesc("还有记录未填写有效性");
				return ReturnUtil.fail(rc);
			}
		}
		List<String> id = new ArrayList<>();
		for (int i = 0; i < ids.length; i++) {
			if (ids[i].split(",").length > 1) {
				String[] idArr = ids[i].split(",");
				for (int j = 0; j < idArr.length; j++) {
					id.add(idArr[j]);
				}
			} else {
				id.add(ids[i]);
			}
		}
		icdCDiseaseHospitalDao.submitChecked(id, user.getUserId());
		return ReturnUtil.success();
	}

	public ReturnData submitEdit(String id, String diseaseName, String diseaseCode, String std, String disable,
			String category, User user, String modifyCode, String similarDisease) {
		// 更新疾病名
		// 校验疾病名如果为空,说明该诊断描述就是疾病名称
		if (StringUtils.isBlank(std)) {
			std = diseaseName;
		}
		// 校验是否与国标一致~不一致需要提示
		if (StringUtils.isNotBlank(similarDisease)) {
			// 根据近义词搜索，没有则根据std搜索
			List<IcdDiseaseNationalStd> value = icdDiseaseNationalStdDao.retrieveNationalByDisease(similarDisease);
			if (value == null || value.size() == 0) {
				value = icdDiseaseNationalStdDao.retrieveNationalByDisease(std);
			}
			if (value != null && value.size() > 0) {
				if (!StringUtils.equals(value.get(0).getCategoryName(), category)
						|| !StringUtils.equals(value.get(0).getDisable(), disable)) {
					ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
					rc.setDesc("与国际标准的疾病的一级和disable不一致：" + value.get(0).getCategoryName() + ","
							+ value.get(0).getDisable());
					return ReturnUtil.fail(rc);
				}
			}
		}

		// 判断该id的原来的疾病名是否相同,如果原来就是相同的说明没有进行修改,若原来有不同的疾病名称,说明经过了修改,需要修改同义词的标准词并修改成该疾病名
		IcdDiseaseHospital value = icdCDiseaseHospitalDao.retrieveObjectById(Long.parseLong(id));
		if (value.getStd().equals(std)) {
			// 没有进行疾病名称修改
		} else {
			// 若有同义词,则修改为标准词
			SynonymyWord synonyWord = synonymyWordDao.retrieveDiSynonymyByName(std);
			if (synonyWord != null) {
				SynonymyWord diseaseNameSynonymy = synonymyWordDao.retrieveDiSynonymyByName(diseaseName);
				if (diseaseNameSynonymy != null) {
					// 若diseaseName也有同义词,不在一组内则提示
					if (!(synonyWord.getSynonymyId() + "").equals(diseaseNameSynonymy.getSynonymyId() + "")) {
						ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
						rc.setDesc("————————————————————————————疾病名和诊断描述在两组同义词中");
						return ReturnUtil.fail(rc);
					}
					// 更新为标准词
					Synonymy synonymy = synonymyDao.retrieveObjectById(synonyWord.getSynonymyId());
					if (synonymy != null) {
						String regEx = "[\u4E00-\u9FA5]";
						Pattern p = Pattern.compile(regEx);
						// 判断是中文标准词
						if (p.matcher(std).find()) {
							synonymy.setChineseStd(std);
							synonymy.setStd(std);
						} else {
							synonymy.setEnglishStd(std);
							if (StringUtils.isBlank(synonymy.getChineseStd())) {
								synonymy.setStd(std);
							}
						}
						synonymy.setModifierId(user.getUserId());
						synonymy.setModifierName(user.getRealName());
						synonymyDao.update(synonymy);
						// 修改疾病表中的疾病名称
						updateStd(value.getStd(), std, user);
					}
				} else {
					// 诊断描述不在同义词中,则把诊断描述添加入疾病名的同义词中
					addSynonymy(synonyWord.getSynonymyId(), diseaseName, std, user);
					if (StringUtils.isNotBlank(value.getStd())) {
						updateStd(value.getStd(), std, user);
					} else {
						icdCDiseaseHospitalDao.updateStdByDiseaseName(diseaseName, std);
					}
				}
			} else {
				// 新增同义词
				// 查询原词的同义词
				SynonymyWord diseaseNameSynonymy = synonymyWordDao.retrieveDiSynonymyByName(diseaseName);
				if (diseaseNameSynonymy != null) {
					// 添加入该组同义词
					addSynonymy(diseaseNameSynonymy.getSynonymyId(), std, std, user);
					if (StringUtils.isNotBlank(value.getStd())) {
						updateStd(value.getStd(), std, user);
					} else {
						icdCDiseaseHospitalDao.updateStdByDiseaseName(diseaseName, std);
					}
				} else {
					if (!StringUtils.equals(std, diseaseName)) {
						// 新增一组同义词
						Synonymy newSynonymy = new Synonymy();
						String regEx = "[\u4E00-\u9FA5]";
						Pattern p = Pattern.compile(regEx);
						// 判断是中文标准词
						if (p.matcher(std).find()) {
							newSynonymy.setChineseStd(std);
							newSynonymy.setStd(std);
							newSynonymy.setEnglishStd("");
						} else {
							newSynonymy.setEnglishStd(std);
							newSynonymy.setChineseStd("");
							if (StringUtils.isBlank(newSynonymy.getChineseStd())) {
								newSynonymy.setStd(std);
							}
						}
						newSynonymy.setProperty("di");
						newSynonymy.setModifierId(user.getUserId());
						newSynonymy.setModifierName(user.getRealName());
						newSynonymy.setWords("\t" + std + "\t" + diseaseName + "\t");
						newSynonymy.setType(1);
						long newSynonymyId = synonymyDao.insert(newSynonymy);
						addSynonymyWord(newSynonymyId, std, user);
						addSynonymyWord(newSynonymyId, diseaseName, user);
						if (StringUtils.isNotBlank(value.getStd())) {
							updateStd(value.getStd(), std, user);
						} else {
							icdCDiseaseHospitalDao.updateStdByDiseaseName(diseaseName, std);
						}
					}
				}
			}
		}
		// end

		// 更新disable
		icdCDiseaseHospitalDao.changeDisableSimilarCodeByStd(user.getUserId(), disable, std, similarDisease,
				modifyCode);
		// 如果disable 更新为2 ,那他不存在一级
		if (StringUtils.equals(disable, "2")) {
			category = "";
		}
		// 更新一级
		if (disable.equals("0") || disable.equals("1")) {
			if (StringUtils.isBlank(category)) {
				throw new RuntimeException("提交失败:有效疾病必须有一级");
			}
		}
		String categoryId = "0";
		if (StringUtils.isNotBlank(category)) {
			// 判断是否需要新增一个一级
			List<String> categoryList = icdCDiseaseHospitalDao.retrievesCategory(category);
			if (categoryList != null && categoryList.size() > 1) {
				categoryId = categoryList.get(0);
			} else if (categoryList == null || categoryList.size() == 0) {
				// 需要新增一个一级
				categoryId = icdCDiseaseHospitalDao.insertCategory(category, user.getUserId()) + "";
			}
		}
		icdCDiseaseHospitalDao.updateCategory(categoryId, std);
		icdCDiseaseHospitalDao.initCategory();
		// 初始化章节
		icdCDiseaseHospitalDao.initChapter();
		return ReturnUtil.success();
	}

	private void updateStd(String oldStd, String newStd, User user) {
		// 判断新的疾病名是否已经存在
		List<Map<String, Object>> ids = icdCDiseaseHospitalDao.getStdId(newStd);
		String id = "";
		for (Map<String, Object> temp : ids) {
			id = temp.get("id") + "";
		}
		if (StringUtils.isNotBlank(id)) {
			// 新的疾病名不需要新增
			icdCDiseaseHospitalDao.updateStd(oldStd, id);
		} else {
			// 新增疾病名
			long newId = icdCDiseaseHospitalDao.insertStd(newStd, user.getUserId());
			icdCDiseaseHospitalDao.updateStd(oldStd, newId + "");
		}
	}

	private void addSynonymy(long id, String addWord, String std, User user) {
		Synonymy synonymy = synonymyDao.retrieveObjectById(id);
		synonymy.setWords(synonymy.getWords() + addWord + "\t");
		String regEx = "[\u4E00-\u9FA5]";
		Pattern p = Pattern.compile(regEx);
		// 判断是中文标准词
		if (p.matcher(std).find()) {
			synonymy.setChineseStd(std);
			synonymy.setStd(std);
		} else {
			synonymy.setEnglishStd(std);
			if (StringUtils.isBlank(synonymy.getChineseStd())) {
				synonymy.setStd(std);
			}
		}
		synonymy.setModifierId(user.getUserId());
		synonymy.setModifierName(user.getRealName());
		synonymyDao.update(synonymy);
		addSynonymyWord(synonymy.getId(), addWord, user);
	}

	private void addSynonymyWord(long id, String word, User user) {
		SynonymyWord newSynonymyWord = new SynonymyWord();
		newSynonymyWord.setModifierId(user.getUserId());
		newSynonymyWord.setModifierName(user.getRealName());
		newSynonymyWord.setWord(word);
		newSynonymyWord.setType(1);
		newSynonymyWord.setSynonymyId(id);
		newSynonymyWord.setProperty("di");
		synonymyWordDao.insert(newSynonymyWord);
	}

	public void initHospitalDiseaseStdDisable() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				LOGGER.info("initHospitalDiseaseStdDisable begin");
				icdCDiseaseHospitalDao.initHospitalDiseaseStdDisable();
				LOGGER.info("initHospitalDiseaseStdDisable end");
			}
		}).start();
	}

	private void setExportTitle(Row row, String chapterSequence, String chapterCode, String chapterName,
			String sectionCode, String sectionName, String gpCode, String gpAccessoryCode, String gpName,
			String suborderCode, String suborderAccessoryCode, String suborderName) {
		int j = 0;
		Cell cell = row.createCell(j);
		cell.setCellValue("code");
		j++;
		cell = row.createCell(j);
		cell.setCellValue("修改code");
		j++;
		cell = row.createCell(j);
		cell.setCellValue("诊断描述");
		j++;
		cell = row.createCell(j);
		cell.setCellValue("标准疾病名称");
		j++;
		cell = row.createCell(j);
		cell.setCellValue("近义词");
		j++;
		cell = row.createCell(j);
		cell.setCellValue("一级");
		j++;
		cell = row.createCell(j);
		cell.setCellValue("disable");
		j++;
		if ("1".equals(chapterSequence)) {
			cell = row.createCell(j);
			cell.setCellValue("章序列");
			j++;
		}
		if ("1".equals(chapterCode)) {
			cell = row.createCell(j);
			cell.setCellValue("章code");
			j++;
		}
		if ("1".equals(chapterName)) {
			cell = row.createCell(j);
			cell.setCellValue("章名称");
			j++;
		}
		if ("1".equals(sectionCode)) {
			cell = row.createCell(j);
			cell.setCellValue("节code");
			j++;
		}
		if ("1".equals(sectionName)) {
			cell = row.createCell(j);
			cell.setCellValue("节名称");
			j++;
		}
		if ("1".equals(gpCode)) {
			cell = row.createCell(j);
			cell.setCellValue("类目code");
			j++;
		}
		if ("1".equals(gpAccessoryCode)) {
			cell = row.createCell(j);
			cell.setCellValue("类目附属码");
			j++;
		}
		if ("1".equals(gpName)) {
			cell = row.createCell(j);
			cell.setCellValue("类目名称");
			j++;
		}
		if ("1".equals(suborderCode)) {
			cell = row.createCell(j);
			cell.setCellValue("亚目code");
			j++;
		}
		if ("1".equals(suborderAccessoryCode)) {
			cell = row.createCell(j);
			cell.setCellValue("亚目附属码");
			j++;
		}
		if ("1".equals(suborderName)) {
			cell = row.createCell(j);
			cell.setCellValue("亚目名称");
			j++;
		}
	}

	private void setExportContent(Row row, String chapterSequence, String chapterCode, String chapterName,
			String sectionCode, String sectionName, String gpCode, String gpAccessoryCode, String gpName,
			String suborderCode, String suborderAccessoryCode, String suborderName, IcdDiseaseHospital vocabElement) {
		Cell cell = row.createCell(0);
		cell.setCellValue(vocabElement.getDiseaseCode() + "");
		int j = 1;

		cell = row.createCell(j);
		cell.setCellValue(vocabElement.getModifyCode());
		j++;
		cell = row.createCell(j);
		cell.setCellValue(vocabElement.getDiseaseName());
		j++;
		cell = row.createCell(j);
		cell.setCellValue(vocabElement.getStd());
		j++;
		cell = row.createCell(j);
		cell.setCellValue(vocabElement.getSimilarDisease());
		j++;
		cell = row.createCell(j);
		cell.setCellValue(vocabElement.getCategory());
		j++;
		cell = row.createCell(j);
		cell.setCellValue(vocabElement.getDisable());
		j++;
		if ("1".equals(chapterSequence)) {
			cell = row.createCell(j);
			cell.setCellValue(vocabElement.getChapterSequence());
			j++;
		}
		if ("1".equals(chapterCode)) {
			cell = row.createCell(j);
			cell.setCellValue(vocabElement.getChapterCode());
			j++;
		}
		if ("1".equals(chapterName)) {
			cell = row.createCell(j);
			cell.setCellValue(vocabElement.getChapterName());
			j++;
		}
		if ("1".equals(sectionCode)) {
			cell = row.createCell(j);
			cell.setCellValue(vocabElement.getSectionCode());
			j++;
		}
		if ("1".equals(sectionName)) {
			cell = row.createCell(j);
			cell.setCellValue(vocabElement.getSectionName());
			j++;
		}
		if ("1".equals(gpCode)) {
			cell = row.createCell(j);
			cell.setCellValue(vocabElement.getGpCode());
			j++;
		}
		if ("1".equals(gpAccessoryCode)) {
			cell = row.createCell(j);
			cell.setCellValue(vocabElement.getGpAccessoryCode());
			j++;
		}
		if ("1".equals(gpName)) {
			cell = row.createCell(j);
			cell.setCellValue(vocabElement.getGpName());
			j++;
		}
		if ("1".equals(suborderCode)) {
			cell = row.createCell(j);
			cell.setCellValue(vocabElement.getSuborderCode());
			j++;
		}
		if ("1".equals(suborderAccessoryCode)) {
			cell = row.createCell(j);
			cell.setCellValue(vocabElement.getSuborderAccessoryCode());
			j++;
		}
		if ("1".equals(suborderName)) {
			cell = row.createCell(j);
			cell.setCellValue(vocabElement.getSuborderName());
			j++;
		}
	}

	public void export(HttpServletResponse resp, String chapterSequence, String chapterCode, String chapterName,
			String sectionCode, String sectionName, String gpCode, String gpAccessoryCode, String gpName,
			String suborderCode, String suborderAccessoryCode, String suborderName) throws Exception {
		// 设置respons的头信息
		RespHeaderUtil.setXlsxResp(resp, "疾病维表");

		Workbook workbook = new XSSFWorkbook(); // 创建文档

		// 根据property和keyword获取同义词列表
		Map<String, Object> param = new HashMap<>();
		List<IcdDiseaseHospital> synonymList = icdCDiseaseHospitalDao.retrievesExportList(param);

		Sheet sheet = workbook.createSheet("疾病维表"); // 创建Sheet
		// 循环写入数据
		for (int i = 0; i <= synonymList.size(); i++) {
			Row row = sheet.createRow(i);
			if (i == 0) {
				// 根据选中的设置标题
				setExportTitle(row, chapterSequence, chapterCode, chapterName, sectionCode, sectionName, gpCode,
						gpAccessoryCode, gpName, suborderCode, suborderAccessoryCode, suborderName);
			} else {
				// 根据选中设置正式数据
				IcdDiseaseHospital vocabElement = synonymList.get(i - 1);
				setExportContent(row, chapterSequence, chapterCode, chapterName, sectionCode, sectionName, gpCode,
						gpAccessoryCode, gpName, suborderCode, suborderAccessoryCode, suborderName, vocabElement);

			}

		}

		OutputStream os = resp.getOutputStream();
		workbook.write(os);

		os.close();
		workbook.close();
	}

	public ReturnData hospitalImport(MultipartFile file, User user) throws Exception {

		// 获取同义词构建对象
		Map<String, Object> param = new HashMap<>();
		param.put("type", 1);
		param.put("property", "di");
		List<Synonymy> synonymyList = synonymyDao.retrieveList(param);
		// 同义词对象列表
		Map<String, IcdDiseaseSynonymy> synonymyMap = new HashMap<>();
		// 判断所有一级是不是在同义词中
		Map<String, String> synonymyId = new HashMap<>();
		Map<String, String> tempSynony = new HashMap<>();
		List<String> synId = new ArrayList<>();
		for (Synonymy synonymy : synonymyList) {
			List<String> words = CollectionUtil
					.getListByArray(synonymy.getWords().substring(1, synonymy.getWords().length()).split("\t"));
			IcdDiseaseSynonymy diseaseSynonymy = new IcdDiseaseSynonymy(synonymy.getId() + "", synonymy.getStd(), false,
					words);
			for (String word : words) {
				synonymyMap.put(word, diseaseSynonymy);
				synonymyId.put(word, synonymy.getId() + "");
			}
		}
		// 获取疾病国标的一级和disable
		List<IcdDiseaseNationalStd> nationalList = icdDiseaseNationalStdDao.retrieveAllNationalDisease();
		Map<String, IcdDiseaseNationalStd> nationalMap = new HashMap<>();
		for (IcdDiseaseNationalStd national : nationalList) {
			if (StringUtils.isNotBlank(national.getDetailName())) {
				nationalMap.put(national.getDetailName(), national);
			}
		}

		// 获取文件流，解析文件
		InputStream inputStream = file.getInputStream();

		@SuppressWarnings("resource")
		Workbook workbook = new XSSFWorkbook(inputStream);
		Sheet sheet = workbook.getSheetAt(0);
		Map<String, Map<String, String>> allData = new HashMap<>();
		List<String> allDiseaseName = new ArrayList<>();
		Map<String, String> stdAndCategory = new HashMap<>();
		StringBuffer errMsg = new StringBuffer();
		StringBuffer errMsg2 = new StringBuffer();
		StringBuffer errMsg3 = new StringBuffer();
		StringBuffer errMsg4 = new StringBuffer();
		StringBuffer errMsg5 = new StringBuffer();
		StringBuffer errMsg6 = new StringBuffer();
		StringBuffer errMsg7 = new StringBuffer();
		StringBuffer errMsg8 = new StringBuffer();
		Map<String, String> stdAndDisable = new HashMap<>();
		StringBuffer errMsg9 = new StringBuffer();
		StringBuffer errMsg10 = new StringBuffer();
		StringBuffer errMsg12 = new StringBuffer();
		Map<String, String> stdAndSimilar = new HashMap<>();
		// 收集一级名称校验~一级名称是否会存在一样的同义词
		Set<String> categoryAll = new HashSet<>();
		// 把所有的相同的疾病的code合并起来
		Map<String, HashSet<String>> stdAndCode = new HashMap<>();
		int i = 1;
		// 处理每一行
		for (Row row : sheet) {
			if (i == 1) {
				// 第一行标题
				i++;
				continue;
			}
			// 近义词
			String similarDisease = getCellNum(row, 4);
			// 修改后的code
			String modifyCode = getCellNum(row, 1);
			String diseaseName = getCellNum(row, 2);
			String std = getCellNum(row, 3);
			String disable = getCellNum(row, 6);
			String category = getCellNum(row, 5);
			if (StringUtils.isBlank(std)) {
				if (errMsg3.length() == 0) {
					errMsg3.append("————————————————————————————该疾病的疾病名必须填写：" + diseaseName + ";");
				} else {
					errMsg3.append(diseaseName + ";");
				}
			}
			// 有效的疾病必须填写一级
			if ("0".equals(disable) || "1".equals(disable)) {
				if (StringUtils.isBlank(category)) {
					if (errMsg4.length() == 0) {
						errMsg4.append("————————————————————————————该疾病的一级必须填写：" + diseaseName + ";");
					} else {
						errMsg4.append(diseaseName + ";");
					}
				}
			} else if ("2".equals(disable)) {
				// 无效的疾病没有一级
				category = "";
			}
			if ("3".equals(disable)) {
				if (errMsg5.length() == 0) {
					errMsg5.append("————————————————————————————该疾病的disable不能填写成错误数据3：" + diseaseName + ";");
				} else {
					errMsg5.append(diseaseName + ";");
				}
			}
			// 判断是否疾病名有重复
			if (allDiseaseName.contains(diseaseName)) {
				if (errMsg6.length() == 0) {
					errMsg6.append("————————————————————————————该疾病重复：" + diseaseName + ";");
				} else {
					errMsg6.append(diseaseName + ";");
				}
			} else {
				allDiseaseName.add(diseaseName);
			}
			// 判断一个疾病名是否只有一个disable
			if (StringUtils.isNotBlank(stdAndDisable.get(std))) {
				if (!StringUtils.equals(stdAndDisable.get(std), disable)) {
					if (errMsg9.length() == 0) {
						errMsg9.append("————————————————————————————该疾病名的disable有不同" + std + ";");
					} else {
						errMsg9.append(std + ";");
					}
				}
			} else {
				stdAndDisable.put(std, disable);
			}

			// 判断是否一个疾病名只有一个一级
			if (StringUtils.isNotBlank(stdAndCategory.get(std))) {
				if (!StringUtils.equals(stdAndCategory.get(std), category)) {
					if (errMsg7.length() == 0) {
						errMsg7.append("————————————————————————————该疾病名的一级不同：" + diseaseName + ";");
					} else {
						errMsg7.append(diseaseName + ";");
					}
				}
			} else {
				stdAndCategory.put(std, category);
			}
			ReturnData rtn = checkDataSynonymy(synonymyMap, StringUpperUtil.changeStr(diseaseName), StringUpperUtil.changeStr(std), errMsg,
					errMsg2);
			if (rtn != null) {
				return rtn;
			}

			// 校验一级和disable是否国标的疾病有冲突
			IcdDiseaseNationalStd nationalValue = nationalMap.get(similarDisease) == null ? nationalMap.get(std)
					: nationalMap.get(similarDisease);
			if (nationalValue != null) {
				if (!StringUtils.equals(nationalValue.getCategoryName(), category)
						|| !StringUtils.equals(nationalValue.getDisable(), disable)) {
					if (errMsg8.length() == 0) {
						errMsg8.append("————————————————————————————该列和国标疾病不一致：" + i + "," + nationalValue.getCategoryName() + ","
								+ nationalValue.getDisable() + ";");
					} else {
						errMsg8.append(
								i + "," + nationalValue.getCategoryName() + "," + nationalValue.getDisable() + ";");
					}
				}
			}
			// 判断一个疾病的 近义词是否只有一个
			if (StringUtils.isNotBlank(stdAndSimilar.get(std))) {
				if (!StringUtils.equals(stdAndSimilar.get(std), similarDisease)) {
					if (errMsg12.length() == 0) {
						errMsg12.append("————————————————————————————该疾病名的近义词有不同" + std + ";");
					} else {
						errMsg12.append(std + ";");
					}
				}
			} else {
				stdAndSimilar.put(std, similarDisease);
			}
			// 把所有的std的code加起来
			if (stdAndCode.get(std) != null) {
				HashSet<String> set = stdAndCode.get(std);
				String[] codeArr = modifyCode.split(";");
				for (String code : codeArr) {
					if (StringUtils.isNotBlank(code))
						set.add(code);
				}
			} else {
				HashSet<String> set = new HashSet<>();
				String[] codeArr = modifyCode.split(";");
				for (String code : codeArr) {
					if (StringUtils.isNotBlank(code))
						set.add(code);
				}
				stdAndCode.put(std, set);
			}
			// 收集所有的一级
			categoryAll.add(category);
			Map<String, String> map = new HashMap<>();
			map.put("diseaseName", diseaseName);
			map.put("std", std);
			map.put("disable", disable);
			map.put("category", category);
			map.put("similarDisease", similarDisease);
			map.put("modifyCode", modifyCode);
			allData.put(diseaseName, map);
			i++;
		}

		// 校验所有的一级不会出现同义词
		for (String category : categoryAll) {
			if (synonymyId.get(category) != null) {
				if (synId.contains(synonymyId.get(category))) {
					String temp = tempSynony.get(synonymyId.get(category)) == null ? ""
							: tempSynony.get(synonymyId.get(category));
					if (errMsg10.length() == 0) {
						errMsg10.append("————————————————————————————一级不会出现同义词:" + category + "," + temp + ";");
					} else {
						errMsg10.append(category + "," + temp + ";");
					}
				} else {
					synId.add(synonymyId.get(category));
					tempSynony.put(synonymyId.get(category), category);
				}
			}
		}
		if (errMsg.length() > 0 || errMsg2.length() > 0 || errMsg3.length() > 0 || errMsg4.length() > 0
				|| errMsg5.length() > 0 || errMsg6.length() > 0 || errMsg7.length() > 0 || errMsg10.length() > 0
				|| errMsg9.length() > 0 || errMsg8.length() > 0 || errMsg12.length() > 0) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(errMsg.toString() + "\n" + errMsg2.toString() + "\n" + errMsg3.toString() + "\n"
					+ errMsg4.toString().trim() + "\n" + errMsg5.toString() + "\n" + errMsg6.toString() + "\n"
					+ errMsg7.toString() + "\n" + errMsg10.toString() + "\n" + errMsg9.toString() + "\n"
					+ errMsg8.toString() + "\n" + errMsg12.toString() + "\n");
			return ReturnUtil.fail(rc);
		}
		// 需要加入同义词
		List<IcdDiseaseSynonymy> addSynonymyList = new ArrayList<>();
		Iterator<Map.Entry<String, IcdDiseaseSynonymy>> it = synonymyMap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, IcdDiseaseSynonymy> entry = it.next();
			IcdDiseaseSynonymy synonymy = entry.getValue();
			boolean addFlag = true;
			for (IcdDiseaseSynonymy add : addSynonymyList) {
				if (add == synonymy) {
					addFlag = false;
					break;
				}
			}
			if (addFlag) {
				if (synonymy.getWords().size() > 1) {
					addSynonymyList.add(synonymy);
				}
			}
		}
		// 构建同义词对象
		List<Synonymy> list = new ArrayList<>();
		for (IcdDiseaseSynonymy value : addSynonymyList) {
			Synonymy sy = new Synonymy();
			String regEx = "[\u4E00-\u9FA5]";
			Pattern p = Pattern.compile(regEx);
			// 判断是中文标准词
			if (p.matcher(value.getStd()).find()) {
				sy.setChineseStd(value.getStd());
				sy.setStd(value.getStd());
				sy.setEnglishStd("");
			} else {
				sy.setEnglishStd(value.getStd());
				if (StringUtils.isBlank(sy.getChineseStd())) {
					sy.setStd(value.getStd());
				}
				sy.setChineseStd("");
			}
			sy.setModifierId(user.getUserId());
			sy.setModifierName(user.getRealName());
			List<String> word = value.getWords();
			StringBuffer wordsStr = new StringBuffer();
			wordsStr.append("\t");
			for (String str : word) {
				wordsStr.append(str).append("\t");
			}
			sy.setWords(wordsStr.toString());
			list.add(sy);
		}
		// 查询出所有的未修改之前的记录
		param.clear();
		List<IcdDiseaseHospital> icdDiseases = icdCDiseaseHospitalDao.retrievesExportList(param);
		// 查询所有的一级
		List<Map<String, Object>> allCategoryList = icdCDiseaseHospitalDao.getCategoryAll();
		Map<String, String> categoryAndId = new HashMap<>();
		for (Map<String, Object> categoryMap : allCategoryList) {
			categoryAndId.put(categoryMap.get("category_name").toString(), categoryMap.get("id").toString());
		}
		// 查询所有的std
		List<Map<String, Object>> allStdList = icdCDiseaseHospitalDao.getStdAll();
		Map<String, String> stdAndId = new HashMap<>();
		for (Map<String, Object> stdMap : allStdList) {
			stdAndId.put(stdMap.get("disease_name").toString(), stdMap.get("id").toString());
		}
		for (IcdDiseaseHospital icdDisease : icdDiseases) {
			Map<String, String> value = allData.get(icdDisease.getDiseaseName());
			if (value != null) {
				// 获取modifyCode
				String modifyCode = "";
				if (stdAndCode.get(value.get("std")) != null) {
					HashSet<String> set = stdAndCode.get(value.get("std"));
					for (String code : set) {
						modifyCode += code;
						modifyCode += ";";
					}
				}
				// 只修改更改过的记录
				if (!StringUtils.equals(value.get("std"), icdDisease.getStd())
						|| !StringUtils.equals(value.get("disable"), icdDisease.getDisable())
						|| !StringUtils.equals(value.get("category"), icdDisease.getCategory())
						|| !StringUtils.equals(value.get("similarDisease"), icdDisease.getSimilarDisease())
						|| !StringUtils.equals(modifyCode, icdDisease.getModifyCode())) {
					// 判断疾病名是否存在
					if (StringUtils.isNotBlank(stdAndId.get(value.get("std")))) {
						// 新的疾病名不需要新增
						// 更新stdid
						icdCDiseaseHospitalDao.updateStdId(value.get("diseaseName"), stdAndId.get(value.get("std")),
								user.getUserId());
						icdCDiseaseHospitalDao.updateStdDisable(stdAndId.get(value.get("std")), value.get("disable"),
								user.getUserId(), value.get("similarDisease"), modifyCode);
					} else {
						// 新增疾病名
						long newId = icdCDiseaseHospitalDao.insertStd(value.get("std"), user.getUserId());
						icdCDiseaseHospitalDao.updateStdId(value.get("diseaseName"), newId + "", user.getUserId());
						icdCDiseaseHospitalDao.updateStdDisable(newId + "", value.get("disable"), user.getUserId(),
								value.get("similarDisease"), modifyCode);
						stdAndId.put(value.get("std"), newId + "");
					}
					// 更新一级
					// 判断是否有一级
					if (StringUtils.isNotBlank(value.get("category"))) {
						String categoryId = categoryAndId.get(value.get("category"));
						if (StringUtils.isNotBlank(categoryId)) {
							// 更新一级
							icdCDiseaseHospitalDao.updateCategory(categoryId, value.get("std"));
						} else {
							// 新增一级更新
							String catId = icdCDiseaseHospitalDao.insertCategory(value.get("category"),
									user.getUserId()) + "";
							icdCDiseaseHospitalDao.updateCategory(catId, value.get("std"));
							categoryAndId.put(value.get("category"), catId);
						}
					} else {
						icdCDiseaseHospitalDao.updateCategory("0", value.get("std"));
					}
				}
			}
		}
		// 先删除di同义词
		// 删除数据库对应同义词及分词数据
		Map<String, Object> param1 = new HashMap<>();
		param1.put("type", 1);
		param1.put("property", VocabConstant.ELEMENT_DISEASE);
		synonymyWordDao.deleteByMap(param1);
		synonymyDao.deleteByMap(param1);
		synonymyDao.batchInsert(1, list, VocabConstant.ELEMENT_DISEASE, user.getUserId(), user.getRealName());
		synonymyWordDao.batchInsertBySynonymy(1, VocabConstant.ELEMENT_DISEASE);
		icdCDiseaseHospitalDao.initCategory();
		// 初始化章节
		icdCDiseaseHospitalDao.initChapter();
		return ReturnUtil.success();
	}

	private ReturnData checkDataSynonymy(Map<String, IcdDiseaseSynonymy> synonymyMap, String diseaseName, String std,
			StringBuffer errMsg, StringBuffer errMsg2) {
		// 判断诊断描述是否在同义词中
		if (synonymyMap.get(diseaseName) == null) {
			// 判断疾病名是否在同义词中
			if (synonymyMap.get(std) == null) {
				if (!std.equals(diseaseName)) {
					List<String> words = new ArrayList<>();
					words.add(diseaseName);
					words.add(std);
					IcdDiseaseSynonymy icdDiseaseSynonymy = new IcdDiseaseSynonymy("", std, true, words);
					// 新增一组
					synonymyMap.put(diseaseName, icdDiseaseSynonymy);
					synonymyMap.put(std, icdDiseaseSynonymy);
				} else {
					List<String> words = new ArrayList<>();
					words.add(std);
					IcdDiseaseSynonymy icdDiseaseSynonymy = new IcdDiseaseSynonymy("", std, true, words);
					// 新增一组
					synonymyMap.put(std, icdDiseaseSynonymy);
				}

			} else {
				IcdDiseaseSynonymy icdDiseaseSynonymy = synonymyMap.get(std);
				// 修改过STD
				if (icdDiseaseSynonymy.isModifiedStd()) {
					// 并且std不在进行修改
					if (icdDiseaseSynonymy.getStd().equals(std)) {
						// 把diseaseName添加入同义词
						List<String> words = icdDiseaseSynonymy.getWords();
						words.add(diseaseName);
						icdDiseaseSynonymy.setWords(words);
						synonymyMap.put(diseaseName, icdDiseaseSynonymy);
					} else {
						if (errMsg.length() == 0) {
							errMsg.append("————————————————————————————该疾病的疾病名已经进行修改不能再次修改：");
							errMsg.append(diseaseName + "," + icdDiseaseSynonymy.getStd() + ";");
						} else {
							errMsg.append(diseaseName + "," + icdDiseaseSynonymy.getStd() + ";");
						}
					}
				} else {
					// 没有修改过std
					// 改为修改后的std,然后给diseaseName加入同义词
					icdDiseaseSynonymy.setModifiedStd(true);
					icdDiseaseSynonymy.setStd(std);
					List<String> words = icdDiseaseSynonymy.getWords();
					words.add(diseaseName);
					icdDiseaseSynonymy.setWords(words);
					synonymyMap.put(diseaseName, icdDiseaseSynonymy);
				}
			}
		} else {
			// 诊断描述存在在同义词中
			if (synonymyMap.get(std) == null) {
				IcdDiseaseSynonymy icdDiseaseSynonymy = synonymyMap.get(diseaseName);
				// 修改过STD
				if (icdDiseaseSynonymy.isModifiedStd()) {
					if (errMsg.length() == 0) {
						errMsg.append("————————————————————————————该疾病的疾病名已经进行修改不能再次修改：");
						errMsg.append(diseaseName + "," + icdDiseaseSynonymy.getStd() + ";");
					} else {
						errMsg.append(diseaseName + "," + icdDiseaseSynonymy.getStd() + ";");
					}
				} else {
					// 没有修改过std
					// 改为修改后的std,然后给std加入同义词
					icdDiseaseSynonymy.setModifiedStd(true);
					icdDiseaseSynonymy.setStd(std);
					List<String> words = icdDiseaseSynonymy.getWords();
					words.add(std);
					icdDiseaseSynonymy.setWords(words);
					synonymyMap.put(std, icdDiseaseSynonymy);
				}
			} else {
				IcdDiseaseSynonymy icdDiseaseSynonymy = synonymyMap.get(diseaseName);
				IcdDiseaseSynonymy stdSynonymy = synonymyMap.get(std);
				if (icdDiseaseSynonymy == stdSynonymy) {
					if (!stdSynonymy.getStd().equals(std)) {
						if (stdSynonymy.isModifiedStd()) {
							if (errMsg.length() == 0) {
								errMsg.append("————————————————————————————该疾病的疾病名已经进行修改不能再次修改：");
								errMsg.append(diseaseName + "," + stdSynonymy.getStd() + ";");
							} else {
								errMsg.append(diseaseName + "," + stdSynonymy.getStd() + ";");
							}
						} else {
							// 修改std为现在的
							stdSynonymy.setModifiedStd(true);
							stdSynonymy.setStd(std);
						}
					} else {
						// 修改std为现在的
						stdSynonymy.setModifiedStd(true);
						stdSynonymy.setStd(std);
					}
				} else {
					if (StringUtils.isBlank(icdDiseaseSynonymy.getId())
							&& StringUtils.isNotBlank(stdSynonymy.getId())) {
						if (icdDiseaseSynonymy.isModifiedStd()) {
							if (icdDiseaseSynonymy.getStd().equals(std)) {
								if (stdSynonymy.isModifiedStd()) {
									if (stdSynonymy.getStd().equals(std)) {
										stdSynonymy.setModifiedStd(true);
										stdSynonymy.setStd(std);
										List<String> words = stdSynonymy.getWords();
										words.addAll(icdDiseaseSynonymy.getWords());
										stdSynonymy.setWords(words);
										// 遍历所有的同义词对象保证对象一致
										for (String word : words) {
											synonymyMap.put(word, stdSynonymy);
										}
									} else {
										if (errMsg.length() == 0) {
											errMsg.append("————————————————————————————该疾病的疾病名已经进行修改不能再次修改：");
											errMsg.append(diseaseName + "," + stdSynonymy.getStd() + ";");
										} else {
											errMsg.append(diseaseName + "," + stdSynonymy.getStd() + ";");
										}
									}
								} else {
									stdSynonymy.setModifiedStd(true);
									stdSynonymy.setStd(std);
									List<String> words = stdSynonymy.getWords();
									words.addAll(icdDiseaseSynonymy.getWords());
									stdSynonymy.setWords(words);
									// 遍历所有的同义词对象保证对象一致
									for (String word : words) {
										synonymyMap.put(word, stdSynonymy);
									}
								}
							} else {
								if (errMsg.length() == 0) {
									errMsg.append("————————————————————————————该疾病的疾病名已经进行修改不能再次修改：");
									errMsg.append(diseaseName + "," + icdDiseaseSynonymy.getStd() + ";");
								} else {
									errMsg.append(diseaseName + "," + icdDiseaseSynonymy.getStd() + ";");
								}
							}
						} else {
							if (stdSynonymy.isModifiedStd()) {
								if (stdSynonymy.getStd().equals(std)) {
									stdSynonymy.setModifiedStd(true);
									stdSynonymy.setStd(std);
									List<String> words = stdSynonymy.getWords();
									words.addAll(icdDiseaseSynonymy.getWords());
									stdSynonymy.setWords(words);
									// 遍历所有的同义词对象保证对象一致
									for (String word : words) {
										synonymyMap.put(word, stdSynonymy);
									}
								} else {
									if (errMsg.length() == 0) {
										errMsg.append("————————————————————————————该疾病的疾病名已经进行修改不能再次修改：");
										errMsg.append(diseaseName + "," + stdSynonymy.getStd() + ";");
									} else {
										errMsg.append(diseaseName + "," + stdSynonymy.getStd() + ";");
									}
								}
							} else {
								// 都无修改过,且diseaseName没有id
								stdSynonymy.setModifiedStd(true);
								stdSynonymy.setStd(std);
								List<String> words = stdSynonymy.getWords();
								words.addAll(icdDiseaseSynonymy.getWords());
								stdSynonymy.setWords(words);
								// 遍历所有的同义词对象保证对象一致
								for (String word : words) {
									synonymyMap.put(word, stdSynonymy);
								}
							}
						}
					} else if (StringUtils.isNotBlank(icdDiseaseSynonymy.getId())
							&& StringUtils.isBlank(stdSynonymy.getId())) {
						if (icdDiseaseSynonymy.isModifiedStd()) {
							if (errMsg.length() == 0) {
								errMsg.append("————————————————————————————该疾病的疾病名已经进行修改不能再次修改：");
								errMsg.append(diseaseName + "," + icdDiseaseSynonymy.getStd() + ";");
							} else {
								errMsg.append(diseaseName + "," + icdDiseaseSynonymy.getStd() + ";");
							}
						} else {
							if (stdSynonymy.isModifiedStd()) {
								if (stdSynonymy.getStd().equals(std)) {
									icdDiseaseSynonymy.setModifiedStd(true);
									icdDiseaseSynonymy.setStd(std);
									List<String> words = icdDiseaseSynonymy.getWords();
									words.addAll(stdSynonymy.getWords());
									icdDiseaseSynonymy.setWords(words);
									// 遍历所有的同义词对象保证对象一致
									for (String word : words) {
										synonymyMap.put(word, icdDiseaseSynonymy);
									}
								} else {
									if (errMsg.length() == 0) {
										errMsg.append("————————————————————————————该疾病的疾病名已经进行修改不能再次修改：");
										errMsg.append(diseaseName + "," + stdSynonymy.getStd() + ";");
									} else {
										errMsg.append(diseaseName + "," + stdSynonymy.getStd() + ";");
									}
								}
							} else {
								icdDiseaseSynonymy.setModifiedStd(true);
								icdDiseaseSynonymy.setStd(std);
								List<String> words = icdDiseaseSynonymy.getWords();
								words.addAll(stdSynonymy.getWords());
								icdDiseaseSynonymy.setWords(words);
								// 遍历所有的同义词对象保证对象一致
								for (String word : words) {
									synonymyMap.put(word, icdDiseaseSynonymy);
								}
							}
						}
					} else if (StringUtils.isBlank(icdDiseaseSynonymy.getId())
							&& StringUtils.isBlank(stdSynonymy.getId())) {
						if (icdDiseaseSynonymy.isModifiedStd()) {
							if (errMsg.length() == 0) {
								errMsg.append("————————————————————————————该疾病的疾病名已经进行修改不能再次修改：");
								errMsg.append(diseaseName + "," + icdDiseaseSynonymy.getStd() + ";");
							} else {
								errMsg.append(diseaseName + "," + icdDiseaseSynonymy.getStd() + ";");
							}
						} else {
							if (stdSynonymy.isModifiedStd()) {
								if (stdSynonymy.getStd().equals(std)) {
									icdDiseaseSynonymy.setModifiedStd(true);
									icdDiseaseSynonymy.setStd(std);
									List<String> words = icdDiseaseSynonymy.getWords();
									words.addAll(stdSynonymy.getWords());
									icdDiseaseSynonymy.setWords(words);
									// 遍历所有的同义词对象保证对象一致
									for (String word : words) {
										synonymyMap.put(word, icdDiseaseSynonymy);
									}
								} else {
									if (errMsg.length() == 0) {
										errMsg.append("————————————————————————————该疾病的疾病名已经进行修改不能再次修改：");
										errMsg.append(diseaseName + "," + stdSynonymy.getStd() + ";");
									} else {
										errMsg.append(diseaseName + "," + stdSynonymy.getStd() + ";");
									}
								}
							} else {
								stdSynonymy.setModifiedStd(true);
								stdSynonymy.setStd(std);
								List<String> words = stdSynonymy.getWords();
								words.addAll(icdDiseaseSynonymy.getWords());
								stdSynonymy.setWords(words);
								// 遍历所有的同义词对象保证对象一致
								for (String word : words) {
									synonymyMap.put(word, stdSynonymy);
								}
							}
						}
					} else {
						if (errMsg2.length() == 0) {
							errMsg2.append("————————————————————————————该疾病的疾病名与诊断描述在不同的同义词中:");
							errMsg2.append(diseaseName + ";");
						} else {
							errMsg2.append(diseaseName + ";");
						}
					}
				}
			}
		}

		return null;

	}

	private String getCellNum(Row row, int num) {
		Cell cell = row.getCell(num);
		String index = "";
		if (cell != null) {
			cell.setCellType(Cell.CELL_TYPE_STRING);
			index = cell.getStringCellValue().trim();
		}
		return index;
	}
}
